package com.tma.mobilechart.line;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class LineAxisCoord2D {
	private FloatBuffer vertexBuffer;
	private float vertices[] ={
			-0.5f,  0.5f,  0.0f,		// y
			-0.5f, -0.5f,  0.0f,		// O
             1.25f,-0.5f,  0.0f			// x
	};
	private float verticesLine2D[] ={
			-0.5f, -0.5f,  0.0f,		// y
			1.25f,-0.5f,  0.0f		// O
	};
	

	public void draw(GL10 gl)
	{
		gl.glFrontFace(GL10.GL_CCW);
		// Enable face culling.
		gl.glEnable(GL10.GL_CULL_FACE);
		// What faces to remove with the face culling.
		gl.glCullFace(GL10.GL_BACK);
		
		setVertices(vertices);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
		gl.glVertexPointer(3,GL10.GL_FLOAT, 0, vertexBuffer);
	    gl.glLineWidth(4.0f);
		gl.glDrawArrays(GL10.GL_LINE_STRIP,0,vertices.length/3);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		
	}
	public void drawSubLine(GL10 gl)
	{
		// a float has 4 bytes so we allocate for each coordinate 4 bytes
		setVertices(verticesLine2D);
		
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glColor4f(0.5f, 1.0f, 0.4f, 1.0f);
		// parameter dau tien chi so he truc toa do 3 (x,y,z)
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		//line strip chi noi thanh 3 diem
		gl.glLineWidth(1.0f);
		gl.glColor4f(0.8f, 0.8f, 0.8f, 1.0f);
		gl.glDrawArrays(GL10.GL_LINES, 0, verticesLine2D.length/3);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	}
	
	 protected void setVertices(float[] vertices) {
		// a float is 4 bytes, therefore we multiply the number if
		// vertices with 4.
		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
	    vertexBuffer = vbb.asFloatBuffer();
	    vertexBuffer.put(vertices);
	    vertexBuffer.position(0);
	}
}